﻿using ETA.Entity;
using ETA.IService;
using ETA.Web.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ETA.Web.Controllers
{
    /// <summary>
    /// 援助事物处理
    /// </summary>
    [AuthFilter("援助事物管理员")]
    public class SosHandlerController : BaseController
    {
        private static IUserService _IUserService { set; get; }
        private static IAskForHelpService _IAskForHelpService { set; get; }

        //
        // GET: /SosHandler/

        public SosHandlerController(IUserService IUserService, IAskForHelpService IAskForHelpService)
        {
            _IUserService = IUserService;
            _IAskForHelpService = IAskForHelpService;
        }
        public ActionResult Index()
        {
            var rolerQueryRes = _IUserService.QueryRoler(new QueryRolerRequest());
            if (rolerQueryRes.Result.IsSuccess)
            {
                ViewBag.Rolers = rolerQueryRes.Rolers;
                ViewBag.FilterRolers = GetRolerDownList(rolerQueryRes.Rolers, "(全部)");
            }
            return View();
        }

        /// <summary>
        /// 返回角色下拉列表
        /// </summary>
        /// <param name="Rolers"></param>
        /// <param name="pref">前缀选项，比如说全部</param>
        /// <returns></returns>
        private List<SelectListItem> GetRolerDownList(IEnumerable<Roler> Rolers, string pref = "")
        {
            var list = new List<SelectListItem>();
            if (!string.IsNullOrEmpty(pref))
            {
                list.Add(new SelectListItem
                {
                    Text = pref,
                    Selected = true,
                    Value = ""
                });
            }
            if (Rolers.Any())
            {
                foreach (var r in Rolers)
                {
                    list.Add(new SelectListItem
                    {
                        Text = r.RolerName,
                        Value = r.RolerId
                    });
                }
            }
            return list;
        }

        public ActionResult HandleSOS(string Sosid)
        {
            Response res = null;
            var count = 0;
            var response = _IAskForHelpService.QueryAskForHelpList(
            new QueryAskForHelpRequest
            {
                Sosid = Sosid
            }, out count
            );
            if (response.Result.IsSuccess)
            {
                var sos = response.AskForHelpList.FirstOrDefault();
                sos.StateEnum = DomainEnum.StateEnum.完毕;
                sos.ModifyDate = DateTime.Now;
                res = _IAskForHelpService.ModifyAskForHelp(new ModifyAskForHelpRequest
                {
                    AskForHelp = sos
                });
            }
            return JsonWithDate(res);
        }


        public ActionResult GetSosById(string Sosid)
        {
            var count = 0;
            HelpReqDTO res = null;
            var list = new List<HelpReqDTO>();
            var response = _IAskForHelpService.QueryAskForHelpList(
            new QueryAskForHelpRequest
            {
                Sosid = Sosid
            }, out count
            );
            if (response.Result.IsSuccess)
            {
                list = new List<HelpReqDTO>();
                foreach (var a in response.AskForHelpList)
                {
                    list.Add(ConverntHelpDto(a));
                }
                res = list.FirstOrDefault();
            }
            return JsonWithDate(res);
        }
        public ActionResult GetSOSList(QueryAskForHelpRequest req)
        {
            var count = 0;
            var list = new List<HelpReqDTO>();
            var response = _IAskForHelpService.QueryAskForHelpList(
            req, out count
            );
            if (response.Result.IsSuccess)
            {
                list = new List<HelpReqDTO>();
                foreach (var a in response.AskForHelpList)
                {
                    list.Add(ConverntHelpDto(a));
                }
            }
            return JsonWithDate(list);
        }

        /// <summary>
        /// 援助数据转换
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private HelpReqDTO ConverntHelpDto(AskForHelp model)
        {
            var submitUser = model.SubmitUser;
            var roler = submitUser.Rolers.First();
            var handlerDate = model.ModifyDate.HasValue ? model.ModifyDate.ToString() : "";
            var dto = new HelpReqDTO
            {
                Category = model.AskForHelpKindEnum + "",
                Type = model.AskForHelpTypeEnum + "",
                Content = model.AskForHelpContent,
                Date = model.CreateDate.ToShortDateString(),
                RecordId = model.AskForHelpId,
                Status = model.StateEnum + "",
                CompanyAddress = "",
                CompanyName = "",
                CompanyPhone = "",
                HandlerDate = handlerDate,
                RolerName = roler.RolerName,
                UserId = submitUser.UserId,
                UserName = submitUser.UserName,
                Sex = submitUser.SexEnum.ToString(),
                Evaluate = model.EvaluateEnum + ""
            };
            return dto;
        }

        public ActionResult GetSOSCount(QueryAskForHelpRequest req)
        {
            var count = 0;
            var response = _IAskForHelpService.QueryAskForHelpList(
            req, out count
            );
            return JsonWithDate(count);
        }

        public class HelpReqDTO
        {
            /// <summary>
            /// 援助记录ID
            /// </summary>
            public string RecordId { get; set; }

            /// <summary>
            /// 事务类型 0 求援
            /// </summary>
            public string Type { set; get; }

            /// <summary>
            /// 事物分类 0 生活类 1工作类 2法律纠纷
            /// </summary>
            public string Category { set; get; }

            /// <summary>
            /// 创建时间
            /// </summary>
            public string Date { get; set; }

            /// <summary>
            /// 状态
            /// </summary>
            public string Status { get; set; }


            /// <summary>
            /// 处理时间
            /// </summary>
            public string HandlerDate { get; set; }


            /// <summary>
            /// 用户id
            /// </summary>
            public string UserId { get; set; }

            /// <summary>
            /// 角色名
            /// </summary>
            public string RolerName { get; set; }

            /// <summary>
            /// 用户姓名
            /// </summary>
            public string UserName { get; set; }


            /// <summary>
            /// 性别
            /// </summary>
            public string Sex { get; set; }

            /// <summary>
            /// 公司名称
            /// </summary>
            public string CompanyName { get; set; }

            /// <summary>
            /// 公司地址
            /// </summary>
            public string CompanyAddress { get; set; }

            /// <summary>
            /// 公司电话
            /// </summary>
            public string CompanyPhone { get; set; }

            public string Content { set; get; }

            public string Evaluate { set; get; }


        }

    }
}
